package hopital.ui;

import hopital.sql.SqlDatabase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;
import javax.swing.JOptionPane;

public class Config extends javax.swing.JDialog {

    private final Properties _property;

    public Config(Properties _property) {
        this.setModal(true);
        this._property=_property;
        initComponents();
        _server.setText(_property.getProperty("ServerName"));
        _port.setText(_property.getProperty("PortNumber"));
        _database.setText(_property.getProperty("DatabaseName"));
        _user.setText(_property.getProperty("UserName"));
        _pass3.setText(_property.getProperty("Password"));
        
        this.setLocationRelativeTo(null);
        setVisible(true);
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        _ok = new javax.swing.JButton();
        _close = new javax.swing.JButton();
        _panel = new javax.swing.JPanel();
        __user = new javax.swing.JLabel();
        __pass = new javax.swing.JLabel();
        __pass1 = new javax.swing.JLabel();
        __pass2 = new javax.swing.JLabel();
        __pass3 = new javax.swing.JLabel();
        _pass3 = new javax.swing.JPasswordField();
        _user = new javax.swing.JTextField();
        _database = new javax.swing.JTextField();
        _port = new javax.swing.JTextField();
        _server = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        jProgressBar1 = new javax.swing.JProgressBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/group41.png")).getImage());
        setMinimumSize(new java.awt.Dimension(450, 200));
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(400, 50));

        _ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/correct8.png"))); // NOI18N
        _ok.setText("Ok");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        _close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/remove11.png"))); // NOI18N
        _close.setText("Fermer");
        _close.setToolTipText("");
        _close.setActionCommand("");
        _close.setAutoscrolls(true);
        _close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _closeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(115, Short.MAX_VALUE)
                .addComponent(_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(_close)
                .addGap(18, 18, 18))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(0, 16, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_close, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        __user.setText("Server");

        __pass.setText("Port");

        __pass1.setText("Database");

        __pass2.setText("UserName");

        __pass3.setText("Password");

        _pass3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _pass3ActionPerformed(evt);
            }
        });

        _server.setToolTipText("");

        javax.swing.GroupLayout _panelLayout = new javax.swing.GroupLayout(_panel);
        _panel.setLayout(_panelLayout);
        _panelLayout.setHorizontalGroup(
            _panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 357, Short.MAX_VALUE)
            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(_panelLayout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, _panelLayout.createSequentialGroup()
                            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(__user)
                                .addComponent(__pass))
                            .addGap(50, 50, 50)
                            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(_port, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                                .addComponent(_server)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, _panelLayout.createSequentialGroup()
                            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(_panelLayout.createSequentialGroup()
                                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(__pass3)
                                        .addComponent(__pass2))
                                    .addGap(29, 29, 29))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, _panelLayout.createSequentialGroup()
                                    .addComponent(__pass1)
                                    .addGap(36, 36, 36)))
                            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(_database, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                                .addComponent(_user, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(_pass3))))
                    .addGap(0, 44, Short.MAX_VALUE)))
        );
        _panelLayout.setVerticalGroup(
            _panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
            .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(_panelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(_server, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(__user))
                    .addGap(12, 12, 12)
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(__pass)
                        .addComponent(_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(__pass1)
                        .addComponent(_database, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(__pass2)
                        .addComponent(_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(__pass3)
                        .addComponent(_pass3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(93, Short.MAX_VALUE)))
        );

        jPanel1.add(_panel, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.add(jProgressBar1);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_END);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__closeActionPerformed
        dispose();
    }//GEN-LAST:event__closeActionPerformed

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        try {
            _property.put("DatabaseName", _database.getText());
            _property.put("Password", _pass3.getText());
            _property.put("PortNumber", _port.getText());
            _property.put("ServerName", _server.getText());
            _property.put("UserName", _user.getText());
            _property.save(new FileOutputStream(new File("CONFIG.")), new Date().toString());
            SqlDatabase.getDatabase().doQuery("SELECT 1/2");
            dispose();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
        
    }//GEN-LAST:event__okActionPerformed

    private void _pass3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__pass3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event__pass3ActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel __pass;
    private javax.swing.JLabel __pass1;
    private javax.swing.JLabel __pass2;
    private javax.swing.JLabel __pass3;
    private javax.swing.JLabel __user;
    private javax.swing.JButton _close;
    private javax.swing.JTextField _database;
    private javax.swing.JButton _ok;
    protected javax.swing.JPanel _panel;
    private javax.swing.JPasswordField _pass3;
    private javax.swing.JTextField _port;
    private javax.swing.JTextField _server;
    private javax.swing.JTextField _user;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
